package com.haredot.config;

import com.alibaba.fastjson2.JSONObject;
import com.haredot.constraint.RabbitConst;
import com.haredot.entity.Logger;
import com.haredot.entity.SignInLog;
import com.haredot.mapper.LoggerMapper;
import com.haredot.mapper.SignInLogMapper;
import com.rabbitmq.client.Channel;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.IOException;

@Configuration
@EnableRabbit
public class RabbitConfig {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(RabbitConfig.class);

    @Resource
    private LoggerMapper loggerMapper ;

    @Resource
    private SignInLogMapper signInLogMapper ;

    @Bean
    public Queue logQueue() {
        return QueueBuilder.durable(RabbitConst.LOG_QUEUE).build();
    }

    @Bean
    public Queue signLogQueue() {
        return QueueBuilder.durable(RabbitConst.SIGN_LOGS_QUEUE).build();
    }

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @RabbitListener(queues=RabbitConst.LOG_QUEUE)
    public void recordLog(Channel channel ,Message message) throws Exception{
        byte[] body = message.getBody();
        String jsonString = new String(body);
        try {
            Logger logger = JSONObject.parseObject(jsonString, Logger.class);
            // 写入表中
            loggerMapper.insert(logger);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

            this.logger.info("消息{}已经写入数据库....", jsonString);

        } catch (IOException e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            this.logger.error("消息{}存储数据库失败", jsonString);
        }
    }


    @RabbitListener(queues=RabbitConst.SIGN_LOGS_QUEUE)
    public void recordSignLog(Channel channel ,Message message) throws Exception{
        byte[] body = message.getBody();
        String jsonString = new String(body);
        try {
            SignInLog logger = JSONObject.parseObject(jsonString, SignInLog.class);
            // 写入表中
            signInLogMapper.insert(logger);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

            this.logger.info("消息{}已经写入数据库....", jsonString);

        } catch (IOException e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            this.logger.error("消息{}存储数据库失败", jsonString);
        }
    }

}
